package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceInspectiontaskService;
import com.eastjet.equipment.service.InspectionDetailsService;
import com.eastjet.maintenance.domain.DeviceInspectiontask;
import com.eastjet.maintenance.domain.DevicePatrolscheme;
import com.eastjet.maintenance.domain.InspectionDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/Task")
public class DeviceInspectiontaskController {

    @Autowired
    private DeviceInspectiontaskService deviceInspectiontaskService;
    @Autowired
    private InspectionDetailsService inspectionDetailsService;

    @GetMapping("/taskList")
    public Page<DeviceInspectiontask> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                      @RequestParam(defaultValue = "2") int size,
                                                      @RequestParam(defaultValue = "") String condition,
                                                      @RequestParam(defaultValue = "")String name
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        return deviceInspectiontaskService.getTaskList(page, size, condition,name);
    }


    @GetMapping("/detail")
    public List<InspectionDetails> selectDetail(@RequestParam(defaultValue = "") String code) {
        return inspectionDetailsService.selectDetail(code);
    }

    @RequestMapping("/updateTask")
    public String updateTask(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("taskmap:" + map);
        DeviceInspectiontask task = new DeviceInspectiontask();
        task.setCode((String) map.get("code"));
        task.setUpdatePerson((String) map.get("name"));
        task.setInspectiontaskTime((String) map.get("inspectiontaskTime"));
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date inspectiontaskOkontime = sdf.parse((String) map.get("inspectiontaskOkontime"));
        Date inspectiontaskOkuntime = sdf.parse((String) map.get("inspectiontaskOkuntime"));
        task.setInspectiontaskOkontime(inspectiontaskOkontime);
        task.setInspectiontaskOkuntime(inspectiontaskOkuntime);
        System.out.println("task:" + task);
        return deviceInspectiontaskService.updateTask(task);
    }

    @RequestMapping("/updateSkip")
    public String updateSkip(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("skipmap:" + map);
        if(map.isEmpty()){
            return "参数不能为空";
        }else{
            DeviceInspectiontask task = new DeviceInspectiontask();
            task.setCode((String) map.get("code"));
            task.setCause((String) map.get("cause"));
            task.setInspectiontaskSkip((Integer) map.get("skipNumber"));
            System.out.println("task:" + task);
            return deviceInspectiontaskService.updateSkip(task);
        }

    }

    @RequestMapping("/adjustTask")
    public String adjustTask(@RequestBody Map<String,Object> map){
        System.out.println("adjustmap:" + map);
        if(map.isEmpty()){
            return "参数不能为空";
        }else{
            DeviceInspectiontask task = new DeviceInspectiontask();
            task.setCode((String) map.get("code"));
            task.setCause((String) map.get("cause"));
            task.setInspectiontaskClass((String) map.get("inspectiontaskClass"));
            task.setInspectiontaskPerson((String) map.get("inspectiontaskPerson"));
            task.setUpdatePerson((String) map.get("name"));
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date inspectiontaskOkontime = sdf.parse((String) map.get("inspectiontaskOkontime"));
                task.setInspectiontaskOkontime(inspectiontaskOkontime);
                Date inspectiontaskOkuntime = sdf.parse((String) map.get("inspectiontaskOkuntime"));
                task.setInspectiontaskOkuntime(inspectiontaskOkuntime);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

            System.out.println("task:" + task);
            return deviceInspectiontaskService.adjustTask(task);
        }
    }

}
